package com.hakgc.rent.web.servlet;

import com.hakgc.rent.entity.District;
import com.hakgc.rent.entity.Street;
import com.hakgc.rent.service.DistrictService;
import com.hakgc.rent.service.StreetService;
import com.hakgc.rent.service.impl.DistrictServiceImpl;
import com.hakgc.rent.service.impl.StreetServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * @author Xing.Jiejian
 * @date 2020/11/14
 */
@WebServlet(name = "StreetServlet",urlPatterns = "/rent/street")
public class StreetServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 根据参数action来区分要执行的操作
        String action = request.getParameter("action");
        // 请求是否非法的标志位
        boolean flag = false;
        String errorMsg = "非法的请求！";
        // 非法的请求直接返回列表页
        if (action != null && !"".equals(action)){

            switch (action) {
                case "add":
                    goAdd(request,response);
                    return;
                case "update":
                    if (flag = goUpdate(request,response,errorMsg)){
                        request.getRequestDispatcher("/rent/street.jsp").forward(request,response);
                        return;
                    }
                    break;
                case "delete":
                    if (flag = delete(request,response,errorMsg)){
                        // 删除成功，提示删除成功，刷新列表页
                        response.setContentType("text/html;charset=utf-8");
                        PrintWriter out = response.getWriter();
                        out.println("<script>");
                        out.println("alert('删除街道成功！');");
                        out.println("location.href='"+request.getContextPath()+"/rent/streets';");
                        out.println("</script>");
                        return;
                    }
                    break;
                default:
                    // 非法请求
                    flag = false;
                    break;
            }
        }
        // 非法的请求，则给予提示然后跳转新页面
        if (!flag){
            request.setAttribute("errorMsg",errorMsg);
            request.setAttribute("url",request.getContextPath()+"/rent/streets");
            request.getRequestDispatcher("/util/tips.jsp").forward(request,response);
        }
    }

    /**
     * 处理删除请求
     * @param request
     * @param response
     * @return false,处理成功；true非法的请求or处理失败
     * @throws ServletException
     * @throws IOException
     */
    public boolean delete(HttpServletRequest request, HttpServletResponse response,String errorMsg) throws ServletException, IOException{
        String strId = request.getParameter("id");
        if (strId==null || "".equals(strId)){
            errorMsg = "街道编号不能空";
            return false;
        }
        int id = Integer.parseInt(strId);
        boolean flag = new StreetServiceImpl().deleteById(id);
        if (!flag){
            errorMsg = "删除街道失败！";
        }
        return flag;
    }

    /**
     * 跳转新增页面的操作
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void goAdd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 跳转新增页面，需要查询所有的区县集合
        DistrictService districtService = new DistrictServiceImpl();
        List<District> districts = districtService.findAll(false);
        request.setAttribute("districts",districts);
        request.setAttribute("action","add");
        request.getRequestDispatcher("/rent/street.jsp").forward(request,response);
    }

    /**
     * 跳转修改页面
     * @param request
     * @param response
     * @return
     * @throws ServletException
     * @throws IOException
     */
    public boolean goUpdate(HttpServletRequest request, HttpServletResponse response,String errorMsg) throws ServletException, IOException {
        String strId = request.getParameter("id");
        if (strId==null || "".equals(strId)){
            errorMsg = "街道编号不能空";
            return false;
        }
        DistrictService districtService = new DistrictServiceImpl();
        List<District> districts = districtService.findAll(false);
        StreetService streetService = new StreetServiceImpl();
        Street street = streetService.findById(Integer.parseInt(strId));

        request.setAttribute("street",street);
        request.setAttribute("districts",districts);
        request.setAttribute("action","update");
        return true;
    }
}
